(function() {
  app.controller('detailCtrl', function($stateParams, $scope, quoteApi, $interval, canvas, kchart, $rootScope, toast, header_m, my_zxg_v, $state, local_m) {
    var detail_codeinfo = $scope.storage.local('detail_codeinfo');

    header_m
      .set({
        title_c: detail_codeinfo.code,
        title2_c: detail_codeinfo.name,
      });

    var k_chart = {
      k_type: 0x21,
      sublist: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    };
    angular.extend(k_chart, detail_codeinfo);

    var fenshi = angular.copy(k_chart);

    $scope.market_tab_actived = '1';
    $scope.wudang_actived = '1';
    $scope.show_min = '分钟';
    canvas.canvas(fenshi);

    $scope.$on('AddressUpdated', function() { // 监听地址变化并获取相应数据
      angular.extend($scope.order_detail, $rootScope.detail_order);
    });

    var p = new quoteApi.QuoteApi.Price0x21({ // 转让单 购买单 数据
      code: k_chart.code,
      market: k_chart.market,
      sublist: [4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 46]
    });

    var d = new quoteApi.QuoteApi.Detail0x26({ // tab里明细
      code: k_chart.code,
      market: k_chart.market,
      length: 16,
    });

    $scope.init = function() {
      quoteApi.query(p).then(function(data) {
        $scope.order_detail = data;

        // 算出进度条百分比
        var buy_arr = [data.buy_num5, data.buy_num4, data.buy_num3, data.buy_num2, data.buy_num1];
        var buy_max = Math.max.apply(null, buy_arr);
        $scope.buy_rate = [];
        angular.forEach(buy_arr, function(item, index) {
          $scope.buy_rate[index] = item / buy_max;
        });
        var sell_arr = [data.sell_num1, data.sell_num2, data.sell_num3, data.sell_num4, data.sell_num5];
        var sell_max = Math.max.apply(null, sell_arr);
        $scope.sell_rate = [];
        angular.forEach(sell_arr, function(item, index) {
          $scope.sell_rate[index] = item / sell_max;
        });
      });

      quoteApi.query(d).then(function(data) {
        $scope.detail = data;
      });
    };
    $scope.go_strategy = function() {
      local_m.set('selected_strategy_options', {});
      $state.go('strategy', { type: $stateParams.type || '' });
    };
    var timer = $interval(function() {
      $scope.init();
    }, 3000);

    $scope.$on('$destroy', function() {
      $interval.cancel(timer);
      timer = null;
    });

    $scope.market_tab = function(index) {
      $scope.market_tab_actived = index;
      $scope.select_m = false;

      if (index === '4') {
        k_chart.k_type = 0x27;
      } else if (index === '5') {
        k_chart.k_type = 0x28;
      } else if (index === '3') {
        k_chart.k_type = 0x21;
      } else if (index === '1') {
        canvas.canvas(fenshi);
        return;
      } else {
        return;
      }
      kchart.canvas2(k_chart, true);
    };

    $scope.select_min = function(param) {
      $scope.market_tab_actived = '6';
      $scope.select_m = false;
      $scope.show_min = param + '分钟';
      if (param === '1') {
        k_chart.k_type = 0X22;
      } else if (param === '5') {
        k_chart.k_type = 0X23;
      } else if (param === '15') {
        k_chart.k_type = 0X24;
      } else if (param === '30') {
        k_chart.k_type = 0X25;
      } else if (param === '60') {
        k_chart.k_type = 0X26;
      } else {
        return;
      }
      kchart.canvas2(k_chart, true);
    };
    var my_zxg;

    my_zxg_v
      .updata()
      .then(function(res) {
        my_zxg = res;
        if (my_zxg_v.check(detail_codeinfo) !== -1) {
          $scope.my_zxg_exist = true;
        }
      });

    $scope.add_zx = function() {
      delete detail_codeinfo.dec;
      my_zxg_v
        .set(detail_codeinfo, { toggle: true })
        .then(function(res) {
          $scope.my_zxg_exist = !$scope.my_zxg_exist;
        });
    };
  });

  app.filter('detail_date', function() { // 日期处理函数
    return function(num) {
      if (!num) return num;
      if (typeof(num) != 'string') {
        num = num.toString();
      }
      if (num.length == 6) {
        return num.substring(0, 2) + ':' + num.substring(2, 4) + ':' + num.substring(4, 6);
      } else {
        return num.substring(0, 1) + ':' + num.substring(1, 3) + ':' + num.substring(3, 5);
      }
    };
  });


  app.filter('detail_unit', function() {
    return function(num) {
      if (!num) return num;
      var numLen = num.toString().length;
      if (numLen > 8) {
        num = num / 100000000;
        num = num.toFixed(2);
        return num + '亿';

      } else if (numLen > 4) {
        num = num / 10000;
        num = num.toFixed(2);
        return num + '万';
      } else {
        return num;
      }
    };
  });
})(app);